#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'classes/TestRecord.php';

// set up the table(s)
AIR2_DBManager::init();
// clean up table from any prior aborted run
$conn = AIR2_DBManager::get_master_connection();
$conn->execute('drop table if exists test');
$conn->execute('delete from tag where tag_ref_type = \'0\'');
Doctrine::createTablesFromArray(array('TestRecord'));

// create some dummy data
$uuid = '1a2b3c4d';
$test_record = new TestRecord();
$test_record->test_string = '1234helloworld';
$test_record->test_uuid = $uuid;
$test_record->save();

// clear any old test tags
$test_tag_name1 = 'new test tag yippee 123';
$test_tag_name2 = 'yet another testing tag 123';
$conn->execute("delete from tag_master where tm_name = '$test_tag_name1' or tm_name = '$test_tag_name2'");

// create an existing test tag
$test_tm2 = new TagMaster();
$test_tm2->tm_type = 'T';
$test_tm2->tm_name = $test_tag_name2;
$test_tm2->save();

plan(44);

// init browser
$browser = new AirHttpTest();
$browser->set_user('testuser', array());

// set to json
$browser->set_content_type(AirHttpTest::$JSON);

// check that tags return a 200
$page = $browser->http_get('/test_crud/'.$uuid.'/tags');
is($browser->resp_code(), 200, "GET json on taggable resource");

/**********************
 * create new tag_master, and tag this record with it via POST
 */
$newtag = array( 'tm_name' => $test_tag_name1 );
ok( $resp = $browser->http_post('/test_crud/'.$uuid.'/tags',
        array('radix' => json_encode($newtag)) ), "POST create/add new tag");
//diag_dump( $resp );
ok( $tag_post = json_decode($resp,true), "JSON decode POST new tag");
is( $tag_post['success'], true, "POST new tag success");
is( $tag_post['radix']['tm_name'], $test_tag_name1, "POST returns original value");
is( $tag_post['radix']['usage'], 1, "POST returns tag_master usage of 1");
ok( is_null($tag_post['radix']['IptcMaster']), "POST returns NULL iptc_id");
ok( $tag_post['radix']['tm_id'], "POST returns new tag_master id");
is( $tag_post['radix']['tm_type'], 'J', "POST returns 'J' tm_type");
ok( $tag_post['radix']['added_dtim'], 'POST returns added dtim');
$tag1_tm_id = $tag_post['radix']['tm_id'];

/***********************
 * tag record via POST, using existing tag
 */
$newtag = array( 'tm_id' => $test_tm2->tm_id );
ok( $resp = $browser->http_post('/test_crud/'.$uuid.'/tags',
        array('radix' => json_encode($newtag)) ), "POST add existing tag");
ok( $tag_post = json_decode($resp,true), "JSON decode POST existing tag");
is( $tag_post['success'], true, "POST existing tag success");
is( $tag_post['radix']['tm_name'], $test_tag_name2, "POST returns existing tag value");

/***********************
 * tag record via POST, using a tag it already has (duplicates!)
 */
$newtag = array( 'tm_id' => $tag1_tm_id );
ok( $resp = $browser->http_post('/test_crud/'.$uuid.'/tags',
        array('radix' => json_encode($newtag)) ), "POST add duplicate tag");
ok( $tag_post = json_decode($resp,true), "JSON decode POST duplicate tag");
is( $tag_post['success'], false, "POST duplicate tag failure");

/***********************
 * tag record via POST, using a tag name it already has
 */
$newtag = array( 'tm_name' => $test_tag_name1 );
ok( $resp = $browser->http_post('/test_crud/'.$uuid.'/tags',
        array('radix' => json_encode($newtag)) ), "POST add duplicate name tag");
ok( $tag_post = json_decode($resp,true), "JSON decode POST duplicate name tag");
is( $tag_post['success'], false, "POST duplicate name tag failure");

/***********************
 * tag record via POST, using tag name it has, but with capital letters
 */
$newtag = array( 'tm_name' => strtoupper($test_tag_name1) );
ok( $resp = $browser->http_post('/test_crud/'.$uuid.'/tags',
        array('radix' => json_encode($newtag)) ), "POST add duplicate mod tag");
ok( $tag_post = json_decode($resp,true), "JSON decode POST duplicate mod tag");
is( $tag_post['success'], false, "POST duplicate name mod failure");

/***********************
 * read (list) tags via GET
 */
ok( $resp = $browser->http_get('/test_crud/'.$uuid.'/tags'), "GET list of tags");
ok( $tag_list = json_decode($resp,true), "JSON decode GET_list");
is( $tag_list['success'], true, "GET_list success");
is( $tag_list['total'], 2, "GET_list total");
is( count($tag_list['radix']), 2, "GET_list - radix contains 2 items");

/**********************
 * remove tag2 via DELETE
 */
ok( $resp = $browser->http_delete('/test_crud/'.$uuid.'/tags/'.$tag1_tm_id)
        , "DELETE tag1" );
ok( $delete = json_decode($resp,true), "JSON decode DELETE tag1" );
is( $delete['success'], true, "DELETE tag1 success" );
ok( $resp = $browser->http_delete('/test_crud/'.$uuid.'/tags/'.$test_tm2->tm_id)
        , "DELETE tag2" );
ok( $delete = json_decode($resp,true), "JSON decode DELETE tag2" );
is( $delete['success'], true, "DELETE tag2 success" );

/**********************
 * verify Tags and TagMasters are gone (Non-IPTC TagMasters removed when usage == 0)
 */
ok( $resp = $browser->http_get('/test_crud/'.$uuid.'/tags'), "GET empty list of tags");
ok( $tag_list = json_decode($resp,true), "JSON decode empty GET_list");
is( $tag_list['success'], true, "GET_list empty success");
is( $tag_list['total'], 0, "GET_list empty total");
is( count($tag_list['radix']), 0, "GET_list empty - radix contains 0 items");


/**********************
 * round 2 --- add an existing tag by name (not tm_id)
 */
$test_tm2 = new TagMaster();
$test_tm2->tm_type = 'T';
$test_tm2->tm_name = $test_tag_name2;
$test_tm2->save();
$newtag = array( 'tm_name' => strtoupper($test_tag_name2) );
ok( $resp = $browser->http_post('/test_crud/'.$uuid.'/tags',
        array('radix' => json_encode($newtag)) ), "POST add existing tag by name");
ok( $tag_post = json_decode($resp,true), "JSON decode POST existing tag by name");
is( $tag_post['success'], true, "POST existing tag by name success");
is( $tag_post['radix']['tm_name'], $test_tag_name2, "POST returns existing tag value");
is( $tag_post['radix']['tm_id'], $test_tm2->tm_id, "POST returns existing tag id");



// CLEANUP
$conn->execute("drop table test");
$conn->execute("delete from tag where tag_tm_id in (select tm_id from tag_master where tm_name = '$test_tag_name1' or tm_name = '$test_tag_name2')");
$conn->execute("delete from tag_master where tm_name = '$test_tag_name1' or tm_name = '$test_tag_name2'");
?>
